package com.farsight.leo.fssmartlock.activities;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.bases.BaseActivity;
import com.farsight.leo.fssmartlock.bases.BaseApplication;
import com.farsight.leo.fssmartlock.db.AuthCodeEntity;
import com.farsight.leo.fssmartlock.db.DeviceEntity;
import com.farsight.leo.fssmartlock.net.OkHttpHelper;
import com.farsight.leo.fssmartlock.net.bases.BaseOkHttpCallback;
import com.farsight.leo.fssmartlock.net.bean.AuthCodeData;
import com.farsight.leo.fssmartlock.net.bean.UserData;
import com.farsight.leo.fssmartlock.utils.DateUtils;
import com.farsight.leo.fssmartlock.utils.LogUtils;
import com.farsight.leo.fssmartlock.utils.ToastUtils;

import java.text.ParseException;

import androidx.appcompat.widget.AppCompatTextView;

public class GenAuthCodeActivity extends BaseActivity {
    private AppCompatTextView mTvCode;

    public static void start(Context context) {
        Intent starter = new Intent(context, GenAuthCodeActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int set_layout_id() {
        return R.layout.activity_gen_auth_code;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mTvCode = findViewById(R.id.activity_gen_auth_code_tv_code);

    }

    @Override
    protected void initListeners() {

    }

    public void genAuthCode(View view) {
        final UserData userData = BaseApplication.getUserData();
        if (userData == null) {
            return;
        }
        DeviceEntity deviceEntity = BaseApplication.getDeviceDb().getDeviceDao().query(userData.getUser_id());
        if (deviceEntity == null) {
            ToastUtils.showToast(getString(R.string.bind_device_first));
            return;
        }
        AuthCodeData authCode = new AuthCodeData();
        authCode.setDevice_id(deviceEntity.getId());
        OkHttpHelper.getInstance().genAuthCode(authCode, new BaseOkHttpCallback<AuthCodeData>() {
            @Override
            public void onError(String err) {
                ToastUtils.showToast(err);
            }

            @Override
            public void onSuccess(AuthCodeData result) {
                ToastUtils.showToast(getString(R.string.gen_auth_code_success));
                mTvCode.setText(result.getAuth_code());
                LogUtils.log("from server->:"+result.toString());
                AuthCodeEntity entity = new AuthCodeEntity();
                entity.setAuthCode(result.getAuth_code());
                entity.setDeviceId(result.getDevice_id());
                entity.setId(result.getId());
                entity.setUserId(userData.getUser_id());
                try {
                    entity.setValidDate(DateUtils.parseDefaultTimeFormatToLong(result.getValid_date()));
                } catch (ParseException e) {
                    e.printStackTrace();
                    ToastUtils.showToast(e.getMessage());
                    return;
                }
                LogUtils.log(entity.toString());
                BaseApplication.getDeviceDb().getAuthCodeDao().insert(entity);

            }
        });

    }
}
